package cn.core.tool.aviator;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 表达式引擎 >>> 减法
 */
public class SubFunc extends AbstractFunction {

  @Override
  public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
    String param1 = FunctionUtils.getStringValue(arg1, env).trim();
    String param2 = FunctionUtils.getStringValue(arg2, env).trim();
    BigDecimal r = new BigDecimal(param1).subtract(new BigDecimal(param2));
    return new AviatorString(r.toPlainString());
  }

  @Override
  public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3, AviatorObject arg4) {
    String param1 = FunctionUtils.getStringValue(arg1, env).trim();
    String param2 = FunctionUtils.getStringValue(arg2, env).trim();
    Integer param3 = FunctionUtils.getNumberValue(arg3, env).intValue();
    Integer param4 = FunctionUtils.getNumberValue(arg4, env).intValue();
    BigDecimal r = new BigDecimal(param1).subtract(new BigDecimal(param2)).setScale(param3, param4);
    return new AviatorString(r.toPlainString());
  }

  @Override
  public String getName() {
    return "sub";
  }

}
